package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.system.mapper.CodeRecordMapper;
import com.ruoyi.system.domain.CodeRecord;
import com.ruoyi.system.domain.HatchetAgent;
import com.ruoyi.system.service.ICodeRecordService;
import com.ruoyi.system.service.IHatchetAgentService;
import com.ruoyi.common.enums.BusinessStatus;
import com.ruoyi.common.support.Convert;
import com.ruoyi.common.utils.StringUtils;

/**
 * 升级码交易记录 服务层实现
 * 
 * @author ruoyi
 * @date 2019-04-08
 */
@Service
public class CodeRecordServiceImpl implements ICodeRecordService {
	@Autowired
	private CodeRecordMapper codeRecordMapper;
	@Autowired
	private IHatchetAgentService agentService;

	/**
	 * 查询升级码交易记录信息
	 * 
	 * @param id
	 *            升级码交易记录ID
	 * @return 升级码交易记录信息
	 */
	@Override
	public CodeRecord selectCodeRecordById(String id) {
		return codeRecordMapper.selectCodeRecordById(id);
	}

	/**
	 * 查询升级码交易记录列表
	 * 
	 * @param codeRecord
	 *            升级码交易记录信息
	 * @return 升级码交易记录集合
	 */
	@Override
	public List<CodeRecord> selectCodeRecordList(CodeRecord codeRecord) {
		return codeRecordMapper.selectCodeRecordList(codeRecord);
	}

	/**
	 * 新增升级码交易记录
	 * 
	 * @param codeRecord
	 *            升级码交易记录信息
	 * @return 结果
	 */
	@Override
	public int insertCodeRecord(CodeRecord codeRecord) {
		return codeRecordMapper.insertCodeRecord(codeRecord);
	}

	/**
	 * 修改升级码交易记录
	 * 
	 * @param codeRecord
	 *            升级码交易记录信息
	 * @return 结果
	 */
	@Override
	public int updateCodeRecord(CodeRecord codeRecord) {
		return codeRecordMapper.updateCodeRecord(codeRecord);
	}

	/**
	 * 删除升级码交易记录对象
	 * 
	 * @param ids
	 *            需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int deleteCodeRecordByIds(String ids) {
		return codeRecordMapper.deleteCodeRecordByIds(Convert.toStrArray(ids));
	}

	/**
	 * 给代理商添加升级码
	 */
	@Transactional
	public Map<String, Object> addcode(CodeRecord codeRecord,String isCompany) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (StringUtils.isNotEmpty(codeRecord.getName())) {
			// 通过用户名称查找代理商
			HatchetAgent paramAgent = new HatchetAgent();
			paramAgent.setAgentName(codeRecord.getName());
			List<HatchetAgent> selectHatchetAgentList = agentService.selectHatchetAgentList(paramAgent);
			if (StringUtils.isNotEmpty(selectHatchetAgentList)) {
				HatchetAgent agent = selectHatchetAgentList.get(0);
				// 查找该代理商上级
				HatchetAgent parentAgent = null;
				if ("0".equals(isCompany)) {
					//如果扣公司码
					 parentAgent = agentService.selectHatchetAgentByNo("2800001");
				} else {
					 //判断该代理是否是高级顾问
					if ("1".equals(agent.getLEVEL())) {
						parentAgent = agentService.selectHatchetAgentById(agent.getFreeStatus());
					}else {
						//扣上级码
						parentAgent = agentService.selectHatchetAgentById(agent.getParentId());
					}
					
				}
				
				if (StringUtils.isNotNull(parentAgent)) {
					// 判断上级代理商升级码数是否足够划扣
					if (parentAgent.getFastCount() < codeRecord.getCount()) {
						map.put("status", BusinessStatus.FAIL);
						map.put("msg", "上级的代理商的升级码小于" + codeRecord.getCount() + "个");
						return map;
					} else {
						// 给上级代理商扣码
						int subParentCount = 0;
						subParentCount = parentAgent.getFastCount() - codeRecord.getCount();
						parentAgent.setFastCount(subParentCount);
						int updateParentAgent = agentService.updateHatchetAgent(parentAgent);
						if (updateParentAgent > 0) {
							// 给当前代理商加码
							int subCount = 0;
							subCount = agent.getFastCount() + codeRecord.getCount();
							agent.setFastCount(subCount);
							int updateAgent = agentService.updateHatchetAgent(agent);
							if (updateAgent > 0) {
								//
								String fromId = StringUtils.randomUUID();
								String toId = StringUtils.randomUUID();
								// 创建升级码加码记录
								CodeRecord codeRecord1 = new CodeRecord();
								codeRecord1.setId(toId);
								codeRecord1.setAgentId(agent.getID());
								codeRecord1.setName(agent.getAgentName());
								//操作前升级码余量
								codeRecord1.setField1((subCount-codeRecord.getCount())+"");
								//操作前后升级码余量
								codeRecord1.setField2(subCount+"");
								codeRecord1.setCount(codeRecord.getCount());
								codeRecord1.setPrice(codeRecord.getPrice());
								codeRecord1.setType("0");
								codeRecord1.setFromId(fromId);
								codeRecord1.setCreateTime(new Date());
								int insertCodeRecord1 = codeRecordMapper.insertCodeRecord(codeRecord1);
								if (insertCodeRecord1 > 0) {
									// 创建升级码减码记录
									CodeRecord codeRecord2 = new CodeRecord();
									codeRecord2.setId(fromId);
									codeRecord2.setAgentId(parentAgent.getID());
									codeRecord2.setName(parentAgent.getAgentName());
									codeRecord2.setCount(codeRecord.getCount());
									codeRecord2.setPrice(codeRecord.getPrice());
									//操作前升级码余量
									codeRecord2.setField1((subParentCount+codeRecord.getCount())+"");
									//操作前后升级码余量
									codeRecord2.setField2(subParentCount+"");
									codeRecord2.setType("1");
									codeRecord2.setToId(toId);
									codeRecord2.setCreateTime(new Date());
									int insertcodeRecord2 = codeRecordMapper.insertCodeRecord(codeRecord2);
									if (insertcodeRecord2 > 0) {
										map.put("status", BusinessStatus.SUCCESS);
										map.put("msg", "代理商加码成功");
										return map;
									} else {
										throw new RuntimeException("创建升级码减码记录失败");
									}
								}
							}
						} else {
							map.put("status", BusinessStatus.FAIL);
							map.put("msg", "上级代理商扣码失败");
							return map;
						}
					}
				} else {
					map.put("status", BusinessStatus.FAIL);
					map.put("msg", "找不到该代理商的上级");
					return map;
				}
			} else {
				map.put("status", BusinessStatus.FAIL);
				map.put("msg", "该代理商信息找不到");
				return map;
			}

		} else {
			map.put("status", BusinessStatus.FAIL);
			map.put("msg", "代理商名称不能为空");
			return map;
		}
		return map;
	}

	/**
	 * 详情
	 * @param 查看的代理商升级码记录id
	 */
	public CodeRecord selectCodeRecordDetail(String id) {
		//
		CodeRecord codeRecord = codeRecordMapper.selectCodeRecordById(id);
		CodeRecord codeRecord2 = null; 
		//如果是加码
		if ("0".equals(codeRecord.getType())) {
			codeRecord2 = codeRecordMapper.selectCodeRecordById(codeRecord.getFromId());
			codeRecord.setToName(codeRecord.getName());
			codeRecord.setFromName(codeRecord2.getName());
			if (StringUtils.isEmpty(codeRecord.getMerchantId())) {
				codeRecord.setToUserType("代理商");
			} else {
				codeRecord.setToUserType("商户");
			}
			//如果是减码
		} else if ("1".equals(codeRecord.getType())) {
			codeRecord2 = codeRecordMapper.selectCodeRecordById(codeRecord.getToId());
			codeRecord.setToName(codeRecord2.getName());
			codeRecord.setFromName(codeRecord.getName());
			if (StringUtils.isEmpty(codeRecord2.getMerchantId())) {
				codeRecord.setToUserType("代理商");
			} else {
				codeRecord.setToUserType("商户");
			}
		}
		
		return codeRecord;
	}

	/**
	 * 查询总交易量
	 */
	public Long selectSumCount(CodeRecord codeRecord) {
		Long sumCount = codeRecordMapper.selectSumCount(codeRecord);
		if (StringUtils.isNull(sumCount)) {
			return 0L;
		} else {
			return sumCount;
		}
		
	}

}
